package cn.gcyl.module.biz.controller.admin.specialmeeting;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.specialmeeting.vo.*;
import cn.gcyl.module.biz.dal.dataobject.specialmeeting.SpecialMeetingDO;
import cn.gcyl.module.biz.service.specialmeeting.SpecialMeetingService;

@Tag(name = "管理后台 - 专场会议管理")
@RestController
@RequestMapping("/biz/special-meeting")
@Validated
public class SpecialMeetingController {

    @Resource
    private SpecialMeetingService specialMeetingService;

    @PostMapping("/create")
    @Operation(summary = "创建专场会议管理")
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:create')")
    public CommonResult<Long> createSpecialMeeting(@Valid @RequestBody SpecialMeetingSaveReqVO createReqVO) {
        return success(specialMeetingService.createSpecialMeeting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专场会议管理")
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:update')")
    public CommonResult<Boolean> updateSpecialMeeting(@Valid @RequestBody SpecialMeetingSaveReqVO updateReqVO) {
        specialMeetingService.updateSpecialMeeting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专场会议管理")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:delete')")
    public CommonResult<Boolean> deleteSpecialMeeting(@RequestParam("id") Long id) {
        specialMeetingService.deleteSpecialMeeting(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除专场会议管理")
//                @PreAuthorize("@ss.hasPermission('biz:special-meeting:delete')")
    public CommonResult<Boolean> deleteSpecialMeetingList(@RequestParam("ids") List<Long> ids) {
        specialMeetingService.deleteSpecialMeetingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专场会议管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:query')")
    public CommonResult<SpecialMeetingRespVO> getSpecialMeeting(@RequestParam("id") Long id) {
        SpecialMeetingDO specialMeeting = specialMeetingService.getSpecialMeeting(id);
        return success(BeanUtils.toBean(specialMeeting, SpecialMeetingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专场会议管理分页")
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:query')")
    public CommonResult<PageResult<SpecialMeetingRespVO>> getSpecialMeetingPage(@Valid SpecialMeetingPageReqVO pageReqVO) {
        PageResult<SpecialMeetingDO> pageResult = specialMeetingService.getSpecialMeetingPage(pageReqVO);
        PageResult<SpecialMeetingRespVO> bean = BeanUtils.toBean(pageResult, SpecialMeetingRespVO.class);
        // 根据开始时间和结束时间设置会议状态
        LocalDateTime now = LocalDateTime.now();
        bean.getList().forEach(item -> {
            if (now.isBefore(item.getStartTime())) {
                // 当前时间在开始时间之前，会议未开始
                item.setActiveState(0);
            } else if (now.isAfter(item.getEndTime())) {
                // 当前时间在结束时间之后，会议已结束
                item.setActiveState(2);
            } else {
                // 当前时间在开始时间和结束时间之间，会议进行中
                item.setActiveState(1);
            }
        });
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专场会议管理 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:special-meeting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSpecialMeetingExcel(@Valid SpecialMeetingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpecialMeetingDO> list = specialMeetingService.getSpecialMeetingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专场会议管理.xls", "数据", SpecialMeetingRespVO.class,
                        BeanUtils.toBean(list, SpecialMeetingRespVO.class));
    }

}